package com.atguigu.guli.serviceedu.controller;

import com.atguigu.guli.serviceedu.entity.Course;
import com.atguigu.guli.serviceedu.entity.Teacher;
import com.atguigu.guli.serviceedu.service.CourseService;
import com.atguigu.guli.serviceedu.service.TeacherService;
import com.atguigu.servicebase.result.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "前台教师查询")
@CrossOrigin
@RestController
@RequestMapping("/edu/teacher")
public class TeacherApiController {

    @Autowired
    private TeacherService teacherService;


    @Autowired
    private CourseService courseService;


    //查询前8条热门课程，查询前4条名师
    @Cacheable(value = "teacher-index", key = "'IndexList'")
    @GetMapping("index")
    public R index() {
        //查询前4条名师
        QueryWrapper<Teacher> wrapperTeacher = new QueryWrapper<>();
        wrapperTeacher.orderByDesc("id");
        wrapperTeacher.last("limit 4");
        List<Teacher> teacherList = teacherService.list(wrapperTeacher);
        return R.ok().data("teacherList",teacherList);
    }

    @ApiOperation(value = "分页讲师列表")
    @GetMapping(value = "{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true, example = "1")
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true, example = "8")
            @PathVariable Long limit){
        Page<Teacher> pageParam = new Page<Teacher>(page, limit);
        Map<String, Object> map = teacherService.pageListWeb(pageParam);
        return  R.ok().data(map);
    }

    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping(value = "{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true, example = "1")
            @PathVariable String id){
        //查询讲师信息
        Teacher teacher = teacherService.getById(id);
        //根据讲师id查询这个讲师的课程列表
        List<Course> courseList = courseService.listByTeacherId(id);
        return R.ok().data("teacher", teacher).data("courseList", courseList);
    }
}
